package io.renren.modules.user.controller;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.dao.SysUserRoleDao;
import io.renren.modules.sys.entity.SysUserRoleEntity;
import io.renren.modules.user.entity.vo.PeopleApplyVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.user.entity.TPeopleNoticeEntity;
import io.renren.modules.user.service.TPeopleNoticeService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-09-04 13:58:13
 */
@RestController
@RequestMapping("/tpeoplenotice")
public class TPeopleNoticeController extends AbstractController {
    @Autowired
    private TPeopleNoticeService tPeopleNoticeService;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;
    
    
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:tpeoplenotice:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tPeopleNoticeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("generator:tpeoplenotice:info")
    public R info(@PathVariable("id") Integer id){
		TPeopleNoticeEntity tPeopleNotice = tPeopleNoticeService.getById(id);

        return R.ok().put("tPeopleNotice", tPeopleNotice);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:tpeoplenotice:save")
    public R save(@RequestBody TPeopleNoticeEntity tPeopleNotice){
		tPeopleNoticeService.save(tPeopleNotice);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
  //  @RequiresPermissions("generator:tpeoplenotice:update")
    public R update(@RequestBody TPeopleNoticeEntity tPeopleNotice){
		tPeopleNoticeService.updateById(tPeopleNotice);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:tpeoplenotice:delete")
    public R delete(@RequestBody Integer[] ids){
		tPeopleNoticeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    //查询单位通知信息
    @RequestMapping("/findNotice")
    public R findNotice(String peopleId){
        List<SysUserRoleEntity> sysUserRoleEntities = sysUserRoleDao.queryId(Integer.parseInt(peopleId));
        for(SysUserRoleEntity sysUserRoleEntity:sysUserRoleEntities){
            if("4".equals(sysUserRoleEntity.getRoleId())){
                List<TPeopleNoticeEntity> noticeDept = tPeopleNoticeService.findNoticeDept(null, "4", sysUserRoleEntity.getZdept() + "");
                return R.ok().put("data",noticeDept);
            }else if("6".equals(sysUserRoleEntity.getRoleId())){
                List<TPeopleNoticeEntity> noticeDept = tPeopleNoticeService.findNoticeDept(null, "6", sysUserRoleEntity.getDept() + "");
                return R.ok().put("data",noticeDept);
            }


        }
        return R.ok();
    }


    //查询个人通知信息
    @RequestMapping("/findNoticePeople")
    public R findNoticePeople(@RequestBody TPeopleNoticeEntity noticeEntity){
        List<TPeopleNoticeEntity> noticeDept = tPeopleNoticeService.findNoticeDept(noticeEntity.getPeopleId().toString(), null, null);
        List<TPeopleNoticeEntity> noticeList = tPeopleNoticeService.findNotice(noticeEntity);
        List<TPeopleNoticeEntity> peopleNotice = tPeopleNoticeService.findPeopleNotice(noticeEntity.getPeopleId().toString());
        ArrayList<PeopleApplyVo> array=new ArrayList<>();
        ArrayList<PeopleApplyVo> arrayList=new ArrayList<>();
        ArrayList<PeopleApplyVo> noticList=new ArrayList<>();
      if(noticeDept.size() !=0){
          List<SysUserRoleEntity> sysUserRoleEntities = sysUserRoleDao.queryId(noticeEntity.getPeopleId());
          for(SysUserRoleEntity sysUserRoleEntity:sysUserRoleEntities){
              if("10".equals(sysUserRoleEntity.getRoleId())){
               //  List<TPeopleNoticeEntity> noticeDept = tPeopleNoticeService.findNoticeDept(null, "4", sysUserRoleEntity.getZdept() + "");
                  for (TPeopleNoticeEntity tPeopleNoticeEntity :noticeDept) {
                      PeopleApplyVo people=new PeopleApplyVo();
                      String str=(tPeopleNoticeEntity.getPname()+"从"+tPeopleNoticeEntity.getChanageBeforeName()+"调到"+tPeopleNoticeEntity.getChanageAfterName());
                      people.setStr(str);
                      people.setNoticeId(tPeopleNoticeEntity.getId()+"");
                      array.add(people);
                  }
                 // return R.ok().put("data",noticeDept);
              }else if("6".equals(sysUserRoleEntity.getRoleId())){
                 /* List<TPeopleNoticeEntity> noticeDept = tPeopleNoticeService.findNoticeDept(null, "6", sysUserRoleEntity.getDept() + "");
                  return R.ok().put("data",noticeDept);*/
                  for (TPeopleNoticeEntity tPeopleNoticeEntity :noticeDept) {
                      PeopleApplyVo people=new PeopleApplyVo();
                      String str=(tPeopleNoticeEntity.getPname()+"从"+tPeopleNoticeEntity.getChanageBeforeName()+"调到"+tPeopleNoticeEntity.getChanageAfterName());
                      people.setStr(str);
                      people.setNoticeId(tPeopleNoticeEntity.getId()+"");
                      array.add(people);
                  }
              }
          }
          for (TPeopleNoticeEntity tPeopleNoticeEntity :noticeDept) {
              PeopleApplyVo people=new PeopleApplyVo();
              String str=(tPeopleNoticeEntity.getPname()+"从"+tPeopleNoticeEntity.getChanageBeforeName()+"调到"+tPeopleNoticeEntity.getChanageAfterName());
              people.setStr(str);
              people.setNoticeId(tPeopleNoticeEntity.getId()+"");
              array.add(people);
          }
          for(TPeopleNoticeEntity tPeopleNoticeEntity :noticeList){
              PeopleApplyVo peopleApplyVo=new PeopleApplyVo();
              String str=null;
              if(!"null".equals(tPeopleNoticeEntity.getChanageBeforeName())){
                  str=(tPeopleNoticeEntity.getPname()+"从"+tPeopleNoticeEntity.getChanageBeforeName()+"调到"+tPeopleNoticeEntity.getChanageAfterName());
              }
              if(!"null".equals(tPeopleNoticeEntity.getChanageBeforeNames())){
                   str=(tPeopleNoticeEntity.getPname()+"从"+tPeopleNoticeEntity.getChanageBeforeNames()+"调到"+tPeopleNoticeEntity.getChanageAfterNames());
              }
              peopleApplyVo.setStr(str);
              peopleApplyVo.setNoticeId(tPeopleNoticeEntity.getId()+"");
              peopleApplyVo.setDeptId(tPeopleNoticeEntity.getDeptId());
              arrayList.add(peopleApplyVo);
          }
          for (TPeopleNoticeEntity tPeopleNoticeEntity:peopleNotice) {
              PeopleApplyVo peopleApply=new PeopleApplyVo();
              String str=(tPeopleNoticeEntity.getPname()+"个人信息"+tPeopleNoticeEntity.getChanageAfterClasses());
              peopleApply.setStr(str);
              peopleApply.setNoticeId(tPeopleNoticeEntity.getId()+"");
              noticList.add(peopleApply);
          }
      }else{
          for(TPeopleNoticeEntity tPeopleNoticeEntity :noticeList){
              PeopleApplyVo peopleApplyVo=new PeopleApplyVo();
              String str=null;
              if(tPeopleNoticeEntity.getChanageBeforeName()!=null){
                  str=(tPeopleNoticeEntity.getPname()+"从"+tPeopleNoticeEntity.getChanageBeforeName()+"调到"+tPeopleNoticeEntity.getChanageAfterName());
              }
              if(tPeopleNoticeEntity.getChanageBeforeNames()!=null){
                  str=(tPeopleNoticeEntity.getPname()+"从"+tPeopleNoticeEntity.getChanageBeforeNames()+"调到"+tPeopleNoticeEntity.getChanageAfterNames());
              }
              peopleApplyVo.setStr(str);
              peopleApplyVo.setNoticeId(tPeopleNoticeEntity.getId()+"");
              peopleApplyVo.setDeptId(tPeopleNoticeEntity.getDeptId());
              arrayList.add(peopleApplyVo);
          }
          for (TPeopleNoticeEntity tPeopleNoticeEntity:peopleNotice) {
              PeopleApplyVo peopleApply=new PeopleApplyVo();
              String str=(tPeopleNoticeEntity.getPname()+"个人信息"+tPeopleNoticeEntity.getChanageAfterClasses());
              peopleApply.setStr(str);
              peopleApply.setNoticeId(tPeopleNoticeEntity.getId()+"");
              noticList.add(peopleApply);
          }
      }

      return R.ok().put("data",array).put("arrayList",arrayList).put("noticList",noticList);
    }



}
